package org.osomit.sacct.security.utils;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * A MD5/SHA digest generator
 *
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jun 26, 2009
 */
public class DigestUtil {
    public static String shaDigest(String msg){

        return DigestUtils.shaHex(msg);
    }

    public static String md5Digest(String msg){

        return DigestUtils.md5Hex(msg);
  }

    public static String md5Digest(String[] msgs){
        final String fieldSeparator = ":";

        String digest = null;
        if(msgs.length > 0){
            StringBuffer sb = new StringBuffer();
            sb.append(msgs[0]);
            for(int i=1; i< msgs.length; i++){
                sb.append(fieldSeparator).append(msgs[i]);
            }

            digest = md5Digest(sb.toString());
        }

        return digest;
    }

    public static String shaDigest(String[] msgs){
        final String fieldSeparator = ":";

        String digest = null;
        if(msgs.length > 0){
            StringBuffer sb = new StringBuffer();
            sb.append(msgs[0]);
            for(int i=1; i< msgs.length; i++){
                sb.append(fieldSeparator).append(msgs[i]);
            }

            digest = shaDigest(sb.toString());
        }

        return digest;
    }
}
